package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.db.DB;
import com.vo.Product;

public class ProductDao implements IProduct {

	public List<Product> getAllDate() throws SQLException {
		DB db = new DB();
		Connection con = db.getConn();
		String sql = "SELECT * FROM easybuy_product;";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<Product> list = new ArrayList<Product>();
		while (rs.next()) {
			Product product = new Product();
			product.setEp_id(rs.getInt(1));
			product.setEp_name(rs.getString(2));
			product.setEp_description(rs.getString(3));
			product.setEp_price(rs.getFloat(4));
			product.setEp_stock(rs.getInt(5));
			product.setEpc_id(rs.getInt(6));
			product.setEpc_child_id(rs.getInt(7));
			product.setEp_file_name(rs.getString(8));
			product.setDiscount(rs.getInt(9));
			product.setHot_pro(rs.getInt(10));
			// 求出打折后的价格
			
			product.setNow_price((float) rs.getFloat(4) * rs.getInt(9) / 100);
			list.add(product);
		}
		rs.close();
		ps.close();
		db.closeConn();
		return list;
	}

	//新增商品
	public int insert(Product product) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	//删除商品
	public int detele(int id) throws SQLException {
		// TODO Auto-generated method stub
		DB db = new DB();
		int flag = 0;
		Connection connection = db.getConn();
		String sql = "DELETE FROM easybuy_product WHERE ep_id = "+id;
		PreparedStatement ps = connection.prepareStatement(sql);
		flag = ps.executeUpdate();
		ps.close();
		db.closeConn();
		return flag;
	}

	// 修改商品
	public int update(Product product) throws SQLException {
		// TODO Auto-generated method stub
		DB db = new DB();
		Connection conn = db.getConn();
		String sql = "UPDATE easybuy_product SET ep_name = ? ,epc_id = ? , epc_child_id = ? , ep_price = ? , ep_stock = ? WHERE ep_id = "
				+ product.getEp_id();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, product.getEp_name());
		ps.setInt(2, product.getEpc_id());
		ps.setInt(3, product.getEpc_child_id());
		ps.setFloat(4, product.getEp_price());
		ps.setInt(5, product.getEp_stock());

		int a = ps.executeUpdate();
		ps.close();
		db.closeConn();
		return a;
	}

	public static void main(String[] args) {
		ProductDao dao = new ProductDao();
		int a = 0,id =5;
		/*Product product = new Product();
		product.setEp_id(5);
		product.setEp_name("糖");
		product.setEp_price(564);
		product.setEp_stock(888);
		product.setEpc_id(2);
		product.setEpc_child_id(10);*/
		try {
			//a = dao.detele(id);
			System.out.println(a);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
